/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package tpdisenio2009.services;


import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import tpdisenio2009.Constants;
import tpdisenio2009.model.beans.FantasyTeam;
import tpdisenio2009.model.beans.FantasyTeamGlobalScore;
import tpdisenio2009.model.beans.FantasyTeamPlayer;
import tpdisenio2009.model.beans.FantasyTeamScoreByTournamentDate;
import tpdisenio2009.model.beans.FantasyTeamTransferByTournamentDate;
import tpdisenio2009.model.beans.TournamentDate;
import tpdisenio2009.model.beans.User;
import tpdisenio2009.model.dao.FantasyTeamDAO;
import tpdisenio2009.model.dao.FantasyTeamScoreByTournamentDateDAO;
import tpdisenio2009.model.dao.FantasyTeamTransferByTournamentDateDAO;
import tpdisenio2009.model.dao.TournamentDateDAO;

/**
 *
 * @author Pablito
 */
public class FantasyTeamService {

    private final FantasyTeamDAO fantasyTeamDAO;
    private final FantasyTeamScoreByTournamentDateDAO fantasyTeamScoreDAO;
    private final FantasyTeamTransferByTournamentDateDAO fantasyTeamTrasnferDAO;
    private final TournamentDateDAO tournamentDateDAO;
    
    public FantasyTeamService() {
        fantasyTeamDAO = new FantasyTeamDAO();
        fantasyTeamScoreDAO = new FantasyTeamScoreByTournamentDateDAO();
        fantasyTeamTrasnferDAO = new FantasyTeamTransferByTournamentDateDAO();
        tournamentDateDAO = new TournamentDateDAO();
    }

    public void saveFantasyTeam(FantasyTeam fantasyTeam) {
        fantasyTeamDAO.insert( fantasyTeam );
    }

    public List<FantasyTeam> getAllFantasyTeam() {
        return fantasyTeamDAO.getAll();
    }

    /*
    public List<FantasyTeam> getFantasyTeamsByTournamentDate( TournamentDate date, int page ) {
        return fantasyTeamDAO.getFantasyTeamsByTournamentDate( date, page );
    }*/

    public void updateFantasyTeamScore( Integer id, Integer score ) {
        FantasyTeam team = fantasyTeamDAO.getById( id );
        team.setTotalScore( score + team.getTotalScore() );
        updateFantasyTeam( team );
    }

    public void updateFantasyTeam( FantasyTeam team ) {
        fantasyTeamDAO.update( team );
    }

    public Integer getFantasyTeamTotalPoints( FantasyTeam fantasyTeam ) {
        return fantasyTeamDAO.getById( fantasyTeam.getId() ).getTotalScore();
    }

    public Integer getFantasyTeamPointsByDate( FantasyTeam team, TournamentDate date ) {
        FantasyTeamScoreByTournamentDate ftsbtd = fantasyTeamDAO.getFantasyTeamPointsByDate( team, date );

        if ( ftsbtd == null ) {
            return 0;
        } else {
            return ftsbtd.getScore();
        }
    }

    public Integer getCotization( User user ) {
        List<FantasyTeamPlayer> players = fantasyTeamDAO.getCurrentPlayers( user );
        Integer cotization = 0;
        for ( FantasyTeamPlayer player : players ) {
            cotization += player.getSoccerPlayer().getPrice();
        }
        return cotization;
    }

    public Integer getLlevasGastado( FantasyTeam fantasyTeam ) {
        return fantasyTeamDAO.getLlevasGastado(fantasyTeam);
    }

    public List<FantasyTeamPlayer> getCurrentPlayers( User user ) {
        return fantasyTeamDAO.getCurrentPlayers(user);
    }

    public int getFantasyTeamGlobalPosition( FantasyTeam fantasyTeam ) {
        return fantasyTeamDAO.getFantasyTeamGlobalPosition( fantasyTeam );
    }
    
    public int getFantasyTeamGlobalPositionByDate( FantasyTeam fantasyTeam, TournamentDate date ) {
        List<FantasyTeamGlobalScore> teams = getFantasyTeamsGlobalPositionByDate( date );
        int pos = 1;

        for (FantasyTeamGlobalScore team : teams) {
            if ( team.getFantasyTeam().equals( fantasyTeam ) ) {
                return pos;
            }
            pos++;
        }
        return 0;
    }

    public int getFantasyTeamGlobalPointsByDate( FantasyTeam fantasyTeam, TournamentDate date ) {
        List<FantasyTeamGlobalScore> teams = getFantasyTeamsGlobalPositionByDate( date );

        for (FantasyTeamGlobalScore team : teams) {
            if ( team.getFantasyTeam().equals( fantasyTeam ) ) {
                return team.getScore();
            }
        }
        return 0;
    }

    public List<FantasyTeamGlobalScore> getFantasyTeamsGlobalPositionByDate( TournamentDate date ) {
        List<FantasyTeam> teams = fantasyTeamDAO.getAll();
        List<FantasyTeamGlobalScore> ftGlobalScores = new ArrayList<FantasyTeamGlobalScore>();

        for ( FantasyTeam team : teams ) {
            int totalPoints = 0;
            List<FantasyTeamScoreByTournamentDate> ftScores = fantasyTeamScoreDAO.getFantasyTeamGlobalScoresByTournamentDate(team, date);
            for( FantasyTeamScoreByTournamentDate ftScore : ftScores ) {
                totalPoints = totalPoints + ftScore.getScore();
            }
            ftGlobalScores.add( new FantasyTeamGlobalScore( team, totalPoints ) );
        }
        //Ordena la lista
        Collections.sort(ftGlobalScores);
        
        return ftGlobalScores;
    }

    public List<FantasyTeamGlobalScore> getFantasyTeamsGlobalPositionByDate( TournamentDate date, int page ) {
        List<FantasyTeamGlobalScore> sinPaginar = getFantasyTeamsGlobalPositionByDate( date );
        List<FantasyTeamGlobalScore> paginada = new ArrayList<FantasyTeamGlobalScore>();
        int count = 0;
        for ( FantasyTeamGlobalScore ftScore : sinPaginar ) {
            if ( (count >= ((page - 1)*(Constants.PAGE_SIZE - 1 ))) && (count <= ((page)*(Constants.PAGE_SIZE - 1 ))) ) {
                paginada.add(ftScore);
            }
        }
        return paginada;
    }

    public int getChangesCountInCurrentDate( User user ) {
        TournamentDate date = tournamentDateDAO.getCurrentDate();
        return fantasyTeamDAO.getChangesCountInDate( user, date );
    }    

    //No funca :(
    /*public int getTransfersCountInCurrentDate( User user ) {
        TournamentDate date = tournamentDateDAO.getCurrentDate();
        List<FantasyTeamPlayer> transfers = fantasyTeamDAO.getChangesInDate(user, date);

        int count = 0;

        for( FantasyTeamPlayer currentTransfer : transfers ) {
            for ( FantasyTeamPlayer anyTransfer : transfers ) {
                if( ! currentTransfer.getId().equals(anyTransfer.getId() )
                    && currentTransfer.getSoccerPlayer().equals(anyTransfer.getSoccerPlayer() )
                    && currentTransfer.getBeginDate().equals(anyTransfer.getEndDate() )
                    && ! currentTransfer.getRol().equals(anyTransfer.getRol() ) ) {
                    count++;
                    break;
                    
                } else if ( currentTransfer.getEndDate() != null
                         && ! currentTransfer.getId().equals(anyTransfer.getId() )
                         && currentTransfer.getSoccerPlayer().equals(anyTransfer.getSoccerPlayer() )
                         && currentTransfer.getEndDate().equals(anyTransfer.getBeginDate() )
                         && ! currentTransfer.getRol().equals(anyTransfer.getRol() ) ) {
                        count++;
                        break;
                }
            }
        }        
    } */

    public int getTransfersCountInCurrentDate( User user ) {
        TournamentDate date = tournamentDateDAO.getCurrentDate();
        FantasyTeamTransferByTournamentDate fantasyTeamTransfer = fantasyTeamTrasnferDAO.getTransfersByFantasyTeamAndTournamentDate(user.getFantasyTeam(), date);

        if ( fantasyTeamTransfer != null ) {
            return fantasyTeamTransfer.getTransfersCount();
        } else {
            return 0;
        }
    }

    public boolean existFantasyTeamTransferByTournamentDate( FantasyTeam team, TournamentDate date ){
        return ( fantasyTeamTrasnferDAO.getTransfersByFantasyTeamAndTournamentDate(team, date) != null);
    }

    public void createFantasyTeamTransferByTournamentDate( FantasyTeam team, TournamentDate date, int count ) {
        FantasyTeamTransferByTournamentDate fantasyTeamTransfer = new FantasyTeamTransferByTournamentDate();
        fantasyTeamTransfer.setFantasyTeam(team);
        fantasyTeamTransfer.setTournamentDate(date);
        fantasyTeamTransfer.setTransfersCount(count);
        fantasyTeamTrasnferDAO.insert( fantasyTeamTransfer );
    }

    public FantasyTeamTransferByTournamentDate getFantasyTeamTransferByTournamentDate( FantasyTeam team, TournamentDate date ) {
        return fantasyTeamTrasnferDAO.getTransfersByFantasyTeamAndTournamentDate(team, date);
    }

    public void updateTransfer( FantasyTeamTransferByTournamentDate transfer, int count ) {
        fantasyTeamTrasnferDAO.updateTransfer(transfer, count);
    }

    public boolean existFantasyTeamName( String name ) {
        List<FantasyTeam> clubes = fantasyTeamDAO.getFantasyTeamsByName( name );
         if ( clubes.isEmpty() ) {
             return false;
         } else {
            return true;
         }
    }
}
